package com.soft.service;

import com.soft.dao.*;
import com.soft.entity.*;

import java.util.List;

public class Domain {

    private final UserInfoDao userInfoDao;
    private final AdminDao adminDao;
    private final GoodsDao goodsDao;
    private final GoodsTypeDao goodsTypeDao;
    private final OrderDao orderDao;
    private final OrderInfoDao orderInfoDao;
    private final OrderReturnRecordDao orderReturnRecordDao;
    private final OrderRefundRecordDao orderRefundRecordDao;
    private final TrolleyDao trolleyDao;
    private final TrolleyInfoDao trolleyInfoDao;
    private final MISC misc;

    public Domain() {
        userInfoDao = new UserInfoDao();
        adminDao = new AdminDao();
        goodsDao = new GoodsDao();
        goodsTypeDao = new GoodsTypeDao();
        orderDao = new OrderDao();
        orderInfoDao = new OrderInfoDao();
        orderReturnRecordDao = new OrderReturnRecordDao();
        orderRefundRecordDao = new OrderRefundRecordDao();
        trolleyDao = new TrolleyDao();
        trolleyInfoDao = new TrolleyInfoDao();
        misc = new MISC();
    }

    //获取单个用户
    public UserInfo getUserInfo(String id){
        return userInfoDao.get(id);
    }

    //用户登录
    public boolean UserLogin(String account, String pwd) {
        return userInfoDao.UserLogin(account, pwd);
    }

    //管理员登录
    public boolean AdminLogin(String account, String pwd) {
        return adminDao.AdminLogin(account, pwd);
    }

    //获取全部商品
    public List<Goods> getGoodsAll() {
        return goodsDao.getAll();
    }

    //获取某一类商品全部
    public List<Goods> getGoodsByType(int type) {
        return goodsDao.getByType(type);
    }

    /**
     * @Author: 孔子雄
     * @Description: 根据Id获取商品信息
     * @DateTime: 2021/5/27 8:47
     * @Params: id:商品id
     * @Return: Goods实体类
     */

    public Goods getGoodsById(int id) {
        return goodsDao.get(id);
    }

    //获取全部用户
    public List<UserInfo> getUserAll() {
        return userInfoDao.getAll();
    }

    //更新用户信息
    public boolean updateUserInfo(UserInfo ui) {
        return userInfoDao.Update(ui);
    }

    //注册用户信息
    public boolean registerUserInfo(UserInfo ui) {
        return userInfoDao.Register(ui);
    }

    // 忘记密码
    public boolean forgetPassword(String id,int idType,String idNumber){
        UserInfo ui=getUserInfo(id);
        if(ui.getIdType()==idType && ui.getIdNumber().equals(idNumber)){
            return true;
        }
        return false;
    }

    //获取全部商品分类
    public List<GoodsType> getGoodsTypeAll() {
        return goodsTypeDao.getAll();
    }

    //获取全部订单
    public List<Order> getOrderAll() {
        return orderDao.getAll();
    }

    /**
     * @Author: 孔子雄
     * @Description: 根据orderId查询退货处理记录
     * @DateTime: 2021/5/25 11:13
     * @Params: orderId：订单编号
     * @Return: OrderReturnRecord
     */
    public OrderReturnRecord getByOrderId(int orderId) {
        return orderReturnRecordDao.getByOrderId(orderId);
    }

    /**
     * @Author: 孔子雄
     * @Description: 根据id获取退货处理记录
     * @DateTime: 2021/5/25 21:00
     * @Params: id：记录编号
     * @Return: OrderReturnRecord
     */
    public OrderReturnRecord getById(int id) {
        return orderReturnRecordDao.get(id);
    }

    /**
     * @Author: 孔子雄
     * @Description: 根据id更新退货处理记录
     * @DateTime: 2021/5/25 21:20
     * @Params: ord：退货处理记录实体类
     * @Return: boolean
     */
    public boolean updateOrderReturnRecord(OrderReturnRecord ord) {
        return orderReturnRecordDao.update(ord);
    }

    //插入一条退货记录
    public boolean insertOrderReturnRecord(OrderReturnRecord ord) {
        return orderReturnRecordDao.insert(ord);
    }

    /**
     * @Author: 孔子雄
     * @Description: 根据orderId获取退款记录
     * @DateTime: 2021/5/27 16:45
     * @Params: orderId：订单编号
     * @Return: OrderRefundRecord实体
     */
    public OrderRefundRecord getOrderRefundRecordByOrderId(int orderId) {
        return orderRefundRecordDao.getByOrderId(orderId);
    }

    /**
     * @Author: 孔子雄
     * @Description: 根据id获取退款记录
     * @DateTime: 2021/5/27 17:26
     * @Params: id：编号
     * @Return: OrderRefundRecord实体
     */

    public OrderRefundRecord getOrderRefundRecordById(int id) {
        return orderRefundRecordDao.get(id);
    }

    /**
     * @Author: 孔子雄
     * @Description: 更新OrderRefundRecord
     * @DateTime: 2021/5/27 17:17
     * @Params: ord:OrderRefundRecord实体类
     * @Return: boolean
     */
    public boolean updateOrderRefundRecord(OrderRefundRecord ord) {
        return orderRefundRecordDao.updateOrderRefundRecord(ord);
    }

    /**
     * @Author: 孔子雄
     * @Description: 根据id获取订单
     * @DateTime: 2021/5/31 21:02
     * @Params: id
     * @Return: Order
     */
    public Order getOrderById(int id) {
        return orderDao.get(id);
    }

    /**
     * @Author: 孔子雄
     * @Description:更新order
     * @DateTime: 2021/5/31 21:13
     * @Params:order
     * @Return:boolean
     */
    public boolean updateOrder(Order order) {
        return orderDao.update(order);
    }

    /**
     * @author kongz 2021/6/2 8:31
     * @description 添加商品信息
     * @param goods 商品
     * @return boolean
     */
    public boolean insertGoods(Goods goods) {
        return goodsDao.insert(goods);
    }

    public boolean deleteGoods(int id){

        return goodsDao.deleteGoods(id);
    }
    /**
     * @author 万星月 2021/6/1 20:49
     * @description 更新goods
     * @param goods 商品
     * @return boolean
     */

    public boolean updateGoods(Goods goods) {
        return goodsDao.update(goods);
    }

    /**
     * @author kongz 2021/6/2 14:38
     * @description 冻结or解封用户
     * @param id 用户id
     * @return boolean
     */
    public boolean LockOrFreeUser(String id) {
        UserInfo ui = getUserInfo(id);
        if (ui.getStatus() == UserInfo.STATUS.STATUS_NORMAL)
            ui.setStatus(String.valueOf(UserInfo.STATUS.STATUS_FREEZE));
        else
            ui.setStatus(String.valueOf(UserInfo.STATUS.STATUS_NORMAL));
        return updateUserInfo(ui);
    }

    /**
     * @author kongz 2021/6/2 16:32
     * @description 根据用户获取购物车信息
     * @param userId 用户id
     * @return 购物车信息trolleyinfo列表
     */
    public List<TrolleyInfo> getTrolleyInfoByUser(String userId) {
        Trolley trolley = trolleyDao.getByUser(userId);
        return trolleyInfoDao.getByTrolley(trolley.getId());
    }

    /**
     * @author kongz 2021/6/5 16:21
     * @description 添加商品到购物车
     * @param goodsId 商品id
     * @param userId 用户id
     * @return boolean
     */
    public boolean newTrolleyInfo(int goodsId, String userId) {
        TrolleyInfo trolleyInfo = new TrolleyInfo();
        Goods goods = goodsDao.get(goodsId);
        trolleyInfo.setTrolleyId(String.valueOf(trolleyDao.getByUser(userId).getId()));
        trolleyInfo.setGoodsId(String.valueOf(goodsId));
        trolleyInfo.setGoodsName(goods.getName());
        trolleyInfo.setUnitPrice(String.valueOf(goods.getPrice()));
        trolleyInfo.setCount("1");
        trolleyInfo.setPrice(String.valueOf(trolleyInfo.getUnitPrice() * trolleyInfo.getCount()));
        
        return trolleyInfoDao.insert(trolleyInfo);
    }
    
    /**
     * @author kongz 2021/6/5 16:46
     * @description 获取购物车信息
     * @param id 购物车信息id
     * @return trolleyInfo
     */
    public TrolleyInfo getTrolleyInfoById(int id) {
        return trolleyInfoDao.get(id);
    }
    
    /**
     * @author kongz 2021/6/5 16:53
     * @description 更新购物车信息
     * @param trolleyInfo 购物车信息实体
     * @return boolean
     */
    public boolean updateTrolleyInfo(TrolleyInfo trolleyInfo) {
        trolleyInfo.setPrice(String.valueOf(trolleyInfo.getUnitPrice() * trolleyInfo.getCount()));
        return trolleyInfoDao.update(trolleyInfo);
    }

    /**
     * @author kongz 2021/6/7 16:25
     * @description 根据id删除购物车信息
     * @param id 购物车信息编号
     * @return boolean
     */
    public boolean deleteTrolleyInfo(int id) {
        return trolleyInfoDao.delete(id);
    }

    /**
     * @author kongz 2021/6/7 17:04
     * @description 新建一个订单
     * @param order 订单信息
     * @return boolean
     */
    public boolean newOrder(Order order, String userId) {
        try {
            List<TrolleyInfo> list = getTrolleyInfoByUser(userId);
            order.setTypeNumber(String.valueOf(list.size()));
            order.setTime(misc.getCurrentDate());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return orderDao.insert(order);
    }

    /**
     * @author kongz 2021/6/8 10:00
     * @description 获取用户全部订单
     * @param buyer 付款人
     * @return List<Order>
     */
    public List<Order> getOrderByBuyer(String buyer) {
        return orderDao.getByBuyer(buyer);
    }

    /**
     * @author kongz 2021/6/9 10:43
     * @description 获取退货记录
     * @param id 退货记录编号
     * @return OrderReturnRecord
     */
    public OrderReturnRecord getOrderReturnRecordByOrderId(int id) {
        return orderReturnRecordDao.getByOrderId(id);
    }

    /**
     * @author kongz 2021/6/9 11:07
     * @description
     * @param
     * @return
     */
    public boolean insertOrderRefundRecord(OrderRefundRecord ord) {
        return orderRefundRecordDao.insert(ord);
    }

    /**
     * @author kongz 2021/6/15 10:40
     * @description 获取订单详情
     * @param orderId 订单编号
     * @return List<OrderInfo>
     */
    public List<OrderInfo> getOrderInfoListByOrderId(int orderId) {
        return orderInfoDao.getByOrderId(orderId);
    }

    /**
     * @author kongz 2021/6/15 11:09
     * @description 添加订单详情
     * @param orderInfo 订单详情
     * @return boolean
     */
    public boolean insertOrderInfo(OrderInfo orderInfo) {
        return orderInfoDao.insert(orderInfo);
    }
}
